<?php
/**
 * K米网签名函数
 * @author zhangjie@star-net.cn
 * @copyright © ktvme. All Rights Reserved.
 * @version $Id: kmsign.php 3 2017-05-08 03:02:47Z chendanming $
 */
namespace kmsign;


use think\Config;

class kmsign {
    /**
     * 返回签名md5字符串
     * @param  array $arrParam 签名参数数组
     * @param string $signName 签名字段
     * @return string
     */
    public static function getSignStr($arrInput, $arrAuth) {

        $appsecret         = $arrAuth['appsecret'];
        $appid             = $arrAuth['appid'];
        $arrInput['appid'] = $appid;
        $strTrans          = '';
        if(isset($arrInput['sign'])) {
            unset($arrInput['sign']);
        }
        // 按key值升序排序
        if(ksort($arrInput)) {

            foreach($arrInput as $key => $value) {
                $strVal = trim($value);
                if($strVal === null || $strVal === '') {
                    continue;
                }
                $strTrans .= $key . $strVal;
            }
            $strSign = md5($strTrans . $appsecret);
        }

        return strtoupper($strSign);

    }

    /**
     * 验证签名是否正确
     * @param array $arrParam 签名参数数组
     * @param string $signName 签名字段
     * @return bool
     */
    public static function checkSign($arrInput, $signName = 'auth') {
        $arrAuth = Config::get($signName);
        if(!isset($arrInput['sign'])){
            return false;
        }
        return self::getSignStr($arrInput, $arrAuth) == $arrInput['sign'];
    }
}